package org.jeecg.modules.flow.record.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.aspectj.apache.bcel.classfile.Code;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.modules.flow.record.entity.WmsDocumentTypeStatusUser;
import org.jeecg.modules.flow.record.entity.vo.StatusReminderUserVO;
import org.jeecg.modules.flow.record.mapper.WmsDocumentTypeStatusUserMapper;
import org.jeecg.modules.flow.record.service.IWmsDocumentTypeStatusUserService;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.service.*;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 流程配置提醒消息人发送人
 * @Author: jeecg-boot
 * @Date: 2023-11-30
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class WmsDocumentTypeStatusUserServiceImpl extends ServiceImpl<WmsDocumentTypeStatusUserMapper, WmsDocumentTypeStatusUser> implements IWmsDocumentTypeStatusUserService {

    // 流程消息提醒配置 字典编码
    private static final String dictCode = "flow_msg_reminder";
    private final ISysRoleService sysRoleService;
    private final ISysUserService sysUserService;
    private final ISysUserRoleService sysUserRoleService;
    private final ISysDictService sysDictService;
    private final ISysDictItemService sysDictItemService;

    @Override
    public String getUserName(String reminderId) {

        if (StringUtils.isBlank(reminderId)) {
            return "";
        }
        List<String> userIds = new ArrayList<>();
        WmsDocumentTypeStatusUser statusUser = getOneByReminderId(reminderId);
        if (ObjectUtil.isEmpty(statusUser)) {
            List<DictModel> list = sysDictService.getDictItems(dictCode);
            if (CollectionUtils.isNotEmpty(list)) {
                statusUser = new WmsDocumentTypeStatusUser();
                String ids = list.stream().map(t -> t.getValue()).collect(Collectors.joining(","));
                statusUser.setDocumentUserIds(ids);
                statusUser.setReminderId(reminderId);
                this.save(statusUser);
            }
        }

        if (StringUtils.isNotBlank(statusUser.getRoleIds())) {
            List<String> roleIds = Arrays.stream(statusUser.getRoleIds().split(",")).collect(Collectors.toList());
            List<SysUserRole> sysUserRoles = sysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getRoleId, roleIds));
            List<String> userIds1 = sysUserRoles.stream().map(t -> t.getUserId()).collect(Collectors.toList());
            userIds.addAll(userIds1);
        }

        if (StringUtils.isNotBlank(statusUser.getUserIds())) {
            List<String> userIds2 = Arrays.stream(statusUser.getUserIds().split(",")).collect(Collectors.toList());
            userIds.addAll(userIds2);
        }
        List<String> realNames = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(userIds)) {
            List<SysUser> usesList = sysUserService.list(new LambdaQueryWrapper<SysUser>().in(SysUser::getId, userIds));
            realNames = usesList.stream().map(t -> t.getRealname()).collect(Collectors.toList());
        }

        // 流程设置提醒配置人
        if (StringUtils.isNotBlank(statusUser.getDocumentUserIds())) {
            List<String> userIds3 = Arrays.stream(statusUser.getDocumentUserIds().split(",")).collect(Collectors.toList());
            List<DictModel> list = sysDictService.getDictItems(dictCode);
            List<DictModel> goalList = list.stream().filter(t -> {
                if (userIds3.contains(t.getValue())) {
                    return true;
                }
                return false;
            }).collect(Collectors.toList());
            List<String> texts = goalList.stream().map(t -> t.getText()).collect(Collectors.toList());
            realNames.addAll(texts);
        }
        String names = realNames.stream().distinct().collect(Collectors.joining(","));
        return names;
    }

    @Override
    public Map<String, List<String>> getUserCode(String reminderId) {

        if (StringUtils.isBlank(reminderId)) {
            return MapUtil.empty();
        }
        List<String> userIds = new ArrayList<>();
        WmsDocumentTypeStatusUser statusUser = getOneByReminderId(reminderId);

        if (ObjectUtil.isNotEmpty(statusUser)) {

            Map<String, List<String>> resultMap = new HashMap<>();
            if (StringUtils.isNotBlank(statusUser.getRoleIds())) {
                List<String> roleIds = Arrays.stream(statusUser.getRoleIds().split(",")).collect(Collectors.toList());
                List<SysUserRole> sysUserRoles = sysUserRoleService.list(new LambdaQueryWrapper<SysUserRole>().in(SysUserRole::getRoleId, roleIds));
                List<String> userIds1 = sysUserRoles.stream().map(t -> t.getUserId()).collect(Collectors.toList());
                userIds.addAll(userIds1);
            }

            if (StringUtils.isNotBlank(statusUser.getUserIds())) {
                List<String> userIds2 = Arrays.stream(statusUser.getUserIds().split(",")).collect(Collectors.toList());
                userIds.addAll(userIds2);
            }
            List<String> userNames = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(userIds)) {
                List<SysUser> usesList = sysUserService.list(new LambdaQueryWrapper<SysUser>().in(SysUser::getId, userIds));
                userNames = usesList.stream().map(t -> t.getUsername()).collect(Collectors.toList());
            }
            resultMap.put("userNames", userNames);

            // 流程设置提醒配置人
            if (StringUtils.isNotBlank(statusUser.getDocumentUserIds())) {
                List<String> userIds3 = Arrays.stream(statusUser.getDocumentUserIds().split(",")).collect(Collectors.toList());
                List<DictModel> list = sysDictService.getDictItems(dictCode);
                List<DictModel> goalList = list.stream().filter(t -> {
                    if (userIds3.contains(t.getValue())) {
                        return true;
                    }
                    return false;
                }).collect(Collectors.toList());
                List<String> texts = goalList.stream().map(t -> t.getValue()).collect(Collectors.toList());
                resultMap.put("documentOperators", texts);
            }
            return resultMap;
        }

        return MapUtil.empty();
    }

    @Override
    public WmsDocumentTypeStatusUser getOneByReminderId(String reminderId) {
        WmsDocumentTypeStatusUser statusUser = this.getOne(
                new LambdaQueryWrapper<WmsDocumentTypeStatusUser>().eq(WmsDocumentTypeStatusUser::getReminderId, reminderId));
        return statusUser;
    }

    @Override
    public StatusReminderUserVO getStatusReminderUser(String reminderId) {
        StatusReminderUserVO statusReminderUser = new StatusReminderUserVO();
        WmsDocumentTypeStatusUser statusUser = getOneByReminderId(reminderId);
        if (ObjectUtil.isEmpty(statusUser)) {
            return new StatusReminderUserVO();
        }
        statusReminderUser.setId(statusUser.getId());
        if (StringUtils.isNotBlank(statusUser.getRoleIds())) {
            List<String> roleIds = Arrays.stream(statusUser.getRoleIds().split(",")).collect(Collectors.toList());
            List<SysRole> sysRoles = sysRoleService.list(new LambdaQueryWrapper<SysRole>().in(SysRole::getId, roleIds));
            statusReminderUser.setSysRoles(sysRoles);
        }
        if (StringUtils.isNotBlank(statusUser.getUserIds())) {
            List<SysUser> usesList = sysUserService.list(new LambdaQueryWrapper<SysUser>().in(SysUser::getId, statusUser.getUserIds()));
            statusReminderUser.setSysUsers(usesList);
        }
        if (StringUtils.isNotBlank(statusUser.getDocumentUserIds())) {
            SysDict one = sysDictService.getOne(new LambdaQueryWrapper<SysDict>().eq(SysDict::getDictCode, dictCode));
            if(ObjectUtil.isNotNull(one)){
                List<SysDictItem> list = sysDictItemService.list(new LambdaQueryWrapper<SysDictItem>().eq(SysDictItem::getDictId, one.getId()));
                statusReminderUser.setDictModels(list);
            }
        }

        return statusReminderUser;
    }

    @Override
    public void saveRecord(WmsDocumentTypeStatusUser wmsDocumentTypeStatusUser) {
        this.save(wmsDocumentTypeStatusUser);
    }
}
